//
//  JKRSearchResultViewController.m
//  JKRSearchBar
//
//  Created by tronsis_ios on 17/3/31.
//  Copyright © 2017年 tronsis_ios. All rights reserved.
//

#import "MHSearchCircleResultViewController.h"
#import "MHSearchResultTableViewCell.h"
#import "JKRSearchController.h"
#import "MHSearchCircleModel.h"
#import "MHSearchCircelCell.h"
#import "MHDiscoverHomeModel.h"
#import "MHCommonHeaderView.h"
#import "MHCustomItemViewModel.h"
#import "ImageTitleButton.h"
#import "MHCircleMoreViewModel.h"

@interface MHSearchCircleResultViewController ()< JKRSearchBarDelegate>

@end

@implementation MHSearchCircleResultViewController
@dynamic viewModel;
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView mh_registerNibCell:MHSearchCircelCell.class];
    [self.tableView mh_registerNibCell:MHSearchResultTableViewCell.class];
    self.tableView.estimatedRowHeight= 87;
    self.tableView.rowHeight=UITableViewAutomaticDimension;
    self.tableView.backgroundColor  = [UIColor whiteColor];
    self.tableView.emptyDataSetDelegate = nil;
    self.tableView.emptyDataSetSource = nil;

}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tableView.emptyDataSetDelegate = nil;
    self.tableView.emptyDataSetSource = nil;
}

-(UIEdgeInsets)contentInset
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (UITableViewCell *)tableView:(UITableView *)tableView dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath{
    return [tableView dequeueReusableCellWithIdentifier:indexPath.section == 1 ? @"MHSearchResultTableViewCell":@"MHSearchCircelCell"];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.viewModel.shouldMultiSections)
    {
        if (section == 0) {
            return [self.viewModel.dataSource[section] count] >= 3 ? 3 :  [self.viewModel.dataSource[section] count];
        }
        return [self.viewModel.dataSource[section] count];
    }
    return self.viewModel.dataSource.count;
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    MHCommonHeaderView *headerView = [[MHCommonHeaderView alloc] init];
    ImageTitleButton *btn = [[ImageTitleButton alloc] initWithStyle:EFitTitleLeftImageRight];
    btn.imageSize = CGSizeMake(6, 9);
    btn.tag = 1000 + section;
    [btn setTitle:@"更多" forState:0];
    [btn setImage:[UIImage imageNamed:@"icon_found_more"] forState:0];
    [btn setTitleColor:MHMetionTextColor forState:UIControlStateNormal];
    [headerView addSubview:btn];
    __weak __typeof(self)weakSelf = self;
    [btn bk_whenTapped:^{
        MHCircleMoreViewModel *more = [[MHCircleMoreViewModel alloc] initWithServices:self.viewModel.services params:nil];
        more.searchText = weakSelf.viewModel.searchText;
        [weakSelf.viewModel.services pushViewModel:more animated:YES];
        [self hiddenResultView];
    }];
    
    btn.titleLabel.font = MHRegularFont_12;
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(headerView);
        make.right.equalTo(headerView.mas_right).offset(-20);
    }];
    if (section == 0) {
       btn.hidden = (self.viewModel.searchRecent.count <= 3);
    }else
    {
        btn.hidden = YES;
    }
    headerView.contentLabel.text = section == 0 ? @"圈子":@"主题";
    headerView.height = 20;
    headerView.contentLabel.textColor = MHTextColor333;
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30 ;
}


-(void) hiddenResultView
{
    if (self.pushBlock) {
        self.pushBlock();
    }
}

- (void)configureCell:(MHSearchCircelCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(id)object{
    [cell bindViewModel:object];
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [super tableView:tableView didSelectRowAtIndexPath:indexPath];
   
    
    if (indexPath.section == 0) {
        MHCustomItemViewModel *searchItem = self.viewModel.searchRecent[indexPath.row];
        MHSearchCircleModel *search = searchItem.model;
        MHDiscoverHomeModel *model = [MHDiscoverHomeModel new];
        model.circleId = search.circleId;
        model.isInCircle = search.isInCircle;
        model.circleName = search.title;
        model.payType = search.payType;
        model.circleCode = search.circleCode;
        [AXAppDelegate sharedDelegate].currentDiscoverModel = model;
    }else
    {
        
//        MHSearchCircleModel *search = self.viewModel.datas[indexPath.row];
//        MHDiscoverHomeModel *model = [MHDiscoverHomeModel new];
//        model.circleId = search.circleId;
//        model.isInCircle = search.isInCircle;
//        model.circleName = search.title;
//        model.payType = search.payType;
//        [AXAppDelegate sharedDelegate].currentDiscoverModel = model;
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
           [self hiddenResultView];
    });
 
}
#pragma mark - JKRSearchControllerhResultsUpdating
- (void)updateSearchResultsForSearchController:(JKRSearchController *)searchController {
    NSString *searchText = searchController.searchBar.text;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(SELF CONTAINS %@)", searchText];
}




- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.section == 1) {
         return 80;
    }else
    {
       return  UITableViewAutomaticDimension;
    }

}


#pragma mark - JKRSearchBarDelegate
- (void)searchBarTextDidBeginEditing:(JKRSearchBar *)searchBar {
}






-(void)searchBarTextShouldReturn:(JKRSearchBar *)searchBar
{
    if (searchBar.text.length > 0) {
        [self.view endEditing:YES];
        self.viewModel.searchText = searchBar.text;
        [searchBar resignFirstResponder];
        
        self.viewModel.searchRecent = @[];
        @weakify(self);
        [[self.viewModel.searchCircleCommand execute:nil] subscribeNext:^(NSArray *circles) {
            @strongify(self);
            self.viewModel.searchRecent = circles;
            [self.viewModel.requestRemoteDataCommand execute:@(1)];
//            [self.viewModel didChangeValueForKey:@"dataSource"];
//            self.viewModel.dataSource = self.viewModel.dataSource;
        }];
        
        
        self.tableView.emptyDataSetDelegate = self;
        self.tableView.emptyDataSetSource = self;
    }
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = @"没有搜索到相关圈子";
    
    NSDictionary *attributes = @{NSFontAttributeName: MHRegularFont_15,
                                 NSForegroundColorAttributeName: MHMetionTextColor};
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

- (void)searchBarTextDidEndEditing:(JKRSearchBar *)searchBar {
}



- (void)searchBar:(JKRSearchBar *)searchBar textDidChange:(NSString *)searchText {
}

@end
